/*
 * ThreadUtil.fx
 *
 * Created on Apr 22, 2010, 12:22:11 PM
 */

package fxgreader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.lang.Runnable;
import java.util.concurrent.TimeUnit;
import java.lang.System;
import java.lang.Thread;
import java.lang.InterruptedException;

/**
 * @author perkintang
 */

var pool : ExecutorService;
public function runSeparately(exec : function()) {
    var task: Runnable = Runnable {
         override function run() {
              exec();
         }
    }
    pool = Executors.newSingleThreadExecutor();
    pool.execute(task);
};
public function stopRunningTask(){
    pool.shutdown();
    try {
        if (not pool.awaitTermination(60, TimeUnit.SECONDS)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            if (not pool.awaitTermination(60, TimeUnit.SECONDS))
                System.err.println("Pool did not terminate");
        }
    } catch (e: InterruptedException) {
        pool.shutdownNow();
        Thread.currentThread().interrupt();
}
}